'''使用普通的 xmlrpc.client 模块来连接它'''

from xmlrpc.client import SafeTransport, ServerProxy
import ssl

class VerifyCertSafeTransport(SafeTransport):
    def __init__(self, cafile, certfile=None, keyfile=None):
        SafeTransport.__init__(self)
        self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)   # NOSONAR
        self.context.load_verify_locations(cafile)
        if certfile:
            self.context.load_cert_chain(certfile, keyfile)
        self.context.verify_mode = ssl.CERT_REQUIRED

    # def make_connection(self, host):
    #     # Items in the passed dictionary are passed as keyword
    #     # arguments to the http.client.HTTPSConnection() constructor.
    #     # The context argument allows an ssl.SSLContext instance to
    #     # be passed with information about the SSL configuration

    #     # 参数表示的目录下各项被当为关键字参数传给 http.client.HTTPSConnection() 构造器。
    #     # context 参数可接收带有 SSL 确认的 ssl.SSLContext 实例
    #     s = super().make_connection((host, {'context': self._ssl_context}))
    #     return s
    
s = ServerProxy('https://localhost:20001', 
                                transport=VerifyCertSafeTransport('server_cert.pem',
                                                  'client_cert.pem',
                                                  'client_key.pem'),
                                                  allow_none=True)
s.set('foo','bar')
s.set('spam', [1, 2, 3])
print(f"{s.keys() = }")
print(f"{s.get('foo') = }")
print(f"{s.get('spam') = }")
s.delete('spam')
print(f"{s.exists('spam') = }")